package com.alawn.web.anon;

import org.ocpsoft.rewrite.annotation.Join;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alawn.framework.core.web.faces.controller.FacesController;
import com.alawn.framework.services.enums.UserType;
import com.alawn.framework.services.security.User;
import com.alawn.security.service.UserService;

@Controller("/views/anon/register.faces")
@Join(path = "/anon/register", to = "/views/anon/register.faces")
public class RegisterController extends FacesController {

	@Autowired
	private UserService userService;

	@Override
	public void initPage() {
		User user = new User();
		user.setType(userService.count() == 0 ? UserType.ADMIN : UserType.USER);
		putViewScope("user", user);
	}

	public void onRegister() {
		User user = (User) getViewScope("user");
		userService.saveOrUpdate(user);
		putViewScope("user", user);
	}

}
